import {Component, Input, OnInit} from '@angular/core';
import {MapInfo} from '../lunhui-web-front-dto/MapInfo';

import * as $ from 'jquery';
import {BrowserUtils} from '../lunhui-web-front-utils/BrowserUtils';

@Component({
    selector: 'app-lunhui-web-front-map-scene',
    templateUrl: './lunhui-web-front-map-scene.component.html',
    styleUrls: ['./lunhui-web-front-map-scene.component.scss']
})
export class LunhuiWebFrontMapSceneComponent implements OnInit {

    @Input() mapSceneInfo: Array<MapInfo> = null;

    private mapSceneMap: Map<string, string> = new Map<string, string>();

    constructor() {
    }

    ngOnInit() {
        const result: boolean = BrowserUtils.checkToken();

        if (!result) {
            BrowserUtils.backToTarget('login');
            return;
        }

        this.handleMapSceneInfo(this.mapSceneInfo);
        const domStr = this.setDomStr();

        $('#mapSceneViewContent').prepend(domStr);

    }

    private handleMapSceneInfo(mapSceneInfoList: Array<MapInfo>): void {
        const _this = this;
        let locationKey = '';

        if (_this.mapSceneMap.size > 0) {
            _this.mapSceneMap = new Map<string, string>();
        }

        if (mapSceneInfoList !== null) {
            mapSceneInfoList.forEach(entity => {

                if (entity !== null) {
                    locationKey = `${entity.positionX}${entity.positionY}`;
                    _this.mapSceneMap.set(locationKey, entity.sceneNameAbbreviation);
                }
            });
        }
    }

    private setDomStr(): string {
        let domStr = '';

        let i = 5;
        for (i; i > 0; i-- ) {
            let key = '';
            let val = '';

            domStr += '<div style="padding: 5px 0">';
            domStr += i.toString().concat(' ');
            domStr += ' ';

            let k = 1;
            for (k; k < 6; k++) {
                key = `${k}${i}`;
                val = this.mapSceneMap.get(key);

                if (val) {
                    if (k !== 1 && k < 6) {
                        domStr += '|';
                    }
                    domStr += `<a href="javascript:void(0);">${val}</a>`;
                } else {
                    domStr += '<span style="visibility: hidden"> 你</span>';
                }
            }

            domStr += '</div>';
        }

        return domStr;
    }

}
